﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Diagnostics;
using WebApplication2.Models;
namespace WebApplication2.Controllers
{
    public class HomeController : Controller
    {
        private readonly IHubContext<WebApplication2.Hubs.ChatHub> _hubContext;
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger, IHubContext<WebApplication2.Hubs.ChatHub> hubContext)
        {
            _logger = logger;
            _hubContext = hubContext;
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }
        [HttpPost]
        public async Task<string> setp1([FromBody] dynamic data)
        {
            //dynamic d1 = Newtonsoft.Json.JsonConvert.DeserializeObject(Convert.ToString(data));
            //WebApplication2.Hubs.ChatHub.p1 = d1.td;
            await _hubContext.Clients.All.SendAsync("ReceiveMessage", "server",$"{data.td}" );            
           
            return "ok";
        }
        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}